package com.echo.tagparse;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import edu.stanford.nlp.ling.HasWord;
import edu.stanford.nlp.ling.Sentence;
import edu.stanford.nlp.ling.TaggedWord;
import edu.stanford.nlp.tagger.maxent.MaxentTagger;


public class StanfordPOS {
	
	TreeMap myTreeMap = null;
//	Stack stack = null;
	
	public StanfordPOS()throws Exception{
		FileReader fr =null; 
		String temp = this.getClass().getResource("/").getPath()+"\\com\\echo\\nlp\\models\\left3words-wsj-0-18.tagger";
		File reviewFile = new File(this.getClass().getResource("/").getPath()+"\\com\\echo\\res\\cam.txt");
		fr = new FileReader(reviewFile);
		MaxentTagger tagger = new MaxentTagger(temp);
		myTreeMap = new TreeMap();
		List<Sentence<? extends HasWord>> sentences = MaxentTagger.tokenizeText(new BufferedReader(fr));
		for(Sentence<? extends HasWord> sentence: sentences){
			Sentence<TaggedWord> tSentence = MaxentTagger.tagSentence(sentence);
			String fliter = tSentence.toString(false);
			Pattern expression = Pattern.compile("[a-zA-Z]+/JJ");
			Matcher matcher = expression.matcher(fliter);
			Object word = null;
			while(matcher.find()){
				word = matcher.group();
//				System.out.println(word);
				if(myTreeMap.containsKey(word)){
					Integer count = (Integer)myTreeMap.get(word);
					myTreeMap.put(word, new Integer(count.intValue()+1));
				}else{
					myTreeMap.put(word, new Integer(1));
				}
			}
//			System.out.println(tSentence.toString(false));
		}
	}
	
	public void showFrequency(){
		Iterator iter = myTreeMap.keySet().iterator();
		System.out.println(myTreeMap.size());
		int[] frequentTerm = new int[myTreeMap.size()+1];
		int i=0;
		Object key = null;
		while(iter.hasNext()){
			key = iter.next();
			frequentTerm[i] = (Integer)myTreeMap.get(key);
//			System.out.println((String)key+": "+ myTreeMap.get(key));
			i++;
		}
		quickSort(frequentTerm, 0, myTreeMap.size());
		for(int j=frequentTerm.length-1; j>frequentTerm.length-11; j--){
			if(frequentTerm[j]>0){
	 			System.out.print(frequentTerm[j]+" ");
				Iterator tempIter = myTreeMap.keySet().iterator();
				Object tempKey = null;
				while(tempIter.hasNext()){
	//				System.out.println("it works");
					tempKey = tempIter.next();
					if(frequentTerm[j]==(Integer)myTreeMap.get(tempKey)){
						System.out.println((String)tempKey);
					}
				}
			}else{
				break;
			}
		}
	}
	
//	public void showTopFrequent(){
//		int[] frequentTerm = new int[10];
//		for(int i=0; i<10; i++ ){
//			if(!stack.isEmpty()){
//				int frequent = (Integer)stack.pop();
//				frequentTerm[i] = frequent;
//			}
//		}
//		for(int i=0;i<10;i++){
//			System.out.print(frequentTerm[i]+" ");
//		}
//	}
	
	public void quickSort(int a[], int low, int high){
		int pivot;
		int scanup, scandown, mid;
		if(high-low<=0){
			return;
		}else{
			if(high-low==1){
				if(a[high]<a[low]){
					int temp = a[low];
					a[low] = a[high];
					a[high] = temp;
				}
				return;
			}
			mid=(low+high)/2;
			pivot = a[mid];
			int temp = a[mid];
			a[mid] = a[low];
			a[low] = temp;
			scanup = low+1;
			scandown = high;
			do{
				while(scanup<=scandown&&a[scanup]<=pivot){
					scanup++;
				}
				while(pivot<a[scandown]){
					scandown--;
				}
				if(scanup<scandown){
					int temp1 = a[scanup];
					a[scanup] = a[scandown];
					a[scandown] = temp1;
				}
			}while(scanup<scandown);
			
			a[low] = a[scandown];
			a[scandown] = pivot;
			
			if(low<scandown-1){
				quickSort(a, low, scandown-1);
			}
			if(scandown+1<high){
				quickSort(a, scandown+1, high);
			}
		}
		
	}
	

	public static void main(String[] args)throws Exception{
		StanfordPOS sp = new StanfordPOS();
		sp.showFrequency();
//		sp.showTo|pFrequent();
//		int[] test = {1,2,3,4,7,4,2,7,3,9,56,2,1};
//		sp.quickSort(test, 0, 12);
//		for(int i=0; i<test.length; i++){
//			System.out.print(test[i]+" ");
//		}
	}

}